package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.Request;

import cn.wolfcode.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo,Long id1){
        Employee employee1 = SessionUtil.getEmployee();
        Long id = employee1.getId();
        System.out.println(employee1.isAdmin()+"=============");
        /*  List<Notice> notices =  notices = noticeService.noticeAdmin(employee.getId());*/
        PageInfo<Notice> pageInfo = noticeService.query(qo,id);
        Employee employee = SessionUtil.getEmployee();

        if (employee.isAdmin()) {

        }
       /* model.addAttribute("notices",notices);
*/      model.addAttribute("employee1",employee1);
        model.addAttribute("pageResult", pageInfo);
        Notice notice = noticeService.get(id1);
        model.addAttribute("notice",notice);
        return "notice/list";

    }


    @Request(name="公告通知删除",expression="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @Request(name="公告通知新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }


    @Request(name="公告通知新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice",noticeService.get(id));
        }
        return "notice/input";
    }
    @Request(name="公告通知发布/取消",expression="notice:showOrClear")
    @RequestMapping("/changStatus")
    @ResponseBody
    public String changStatus(Long id){
        noticeService.updataStatus(id);
        return "/notice/list";
    }

    @Request(name="公告通知发布/取消",expression="notice:showOrClear")
    @RequestMapping("/falseStatus")
    @ResponseBody
    public String input(Long id){
        noticeService.falseStatus(id);
        return "/notice/list";
    }


    @RequestMapping("/check")
    @ResponseBody
    public String check(Long id){
        Employee employee1 = SessionUtil.getEmployee();
        Long id1 = employee1.getId();
        System.out.println(id);
        noticeService.insertMsg(id,id1);
        return "/notice/list";
    }
}
